﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;
namespace Web
{
    public partial class admin_useradd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            BindNotice();
        }
        private void BindNotice()
        {
            noticesuccess.Visible = false;
            noticefail.Visible = false;
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            AdminInfo user = createModel();
            if (user.Password != user.Password2) {
                failtips.InnerText = "两次输入的密码不同，请重新输入！";
                ChangeStatus(false);
                return;
            }
            if (user.Password.Length < 6) {
                failtips.InnerText = "为安全考虑，请密码长度大于6位！";
                ChangeStatus(false);
                return;
            }
            if (AdminInfoLogic.GetCountFromUserName(user.UserName) > 0) {
                failtips.InnerText = "已经存在相同用户名的管理原，请重新输入！";
                ChangeStatus(false);
                return;
            }
            
            int isSuccess = AdminInfoLogic.InsertOneAdminInfo(user);

            if (isSuccess == 1)
            {
                ChangeStatus(true);
                return;
            }
            else {
                ChangeStatus(false);
            }

        }
        private AdminInfo createModel()
        {
            AdminInfo user = new AdminInfo();
            user.UserName = txbName.Text.Trim();
            user.Password = txbPassword.Text.Trim();
            user.Password2 = txbPassword2.Text.Trim();
            return user;
        }

        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();divupload();", true);
        }
    }
}
